package repositories

import (
	"errors"
	"github.com/kataras/iris/v12"
	"irisfarm/app/core/db"
	"irisfarm/app/helper"
	"irisfarm/app/models"
)

type AuthRepository struct {
	BaseRepository
}

// GetByUsername 通过用户名获取用户
func (u *AuthRepository) GetByUsername(ctx iris.Context) (models.UsersModel, error) {
	userModel := models.UsersModel{}
	db.Db.Where("username = ?", ctx.PostValue("username")).
		Where("password = ?", helper.Md5(ctx.PostValue("password"))).
		First(&userModel)

	if userModel.ID == 0 {
		return userModel, errors.New("不存在的用户")
	}
	return userModel, nil
}
